package com.zhr.emsg.demo;

import com.smartframework.web.BaseRunner;
import com.smartframework.web.core.mq.bean.MqMsgBean;
import com.zhr.emsg.demo.producer.MsgProducerThread;
import com.zhr.scada.emsg.rpc.EmsgRestClient;

import java.util.HashMap;

import static javafx.application.Platform.exit;

/**
 * @author LIUJUN
 */
public class RunnerMain {
    public static void main(String[] args) {
        BaseRunner.init(RunnerMain.class, args);
        if (args[0].equalsIgnoreCase("p")) {
            //模拟周期性发送Emsg
            new MsgProducerThread().start();
        } else if (args[0].equalsIgnoreCase("c")) {
            //启动消息接受处理程序
            BaseRunner.startMsgRunner();
        } else if (args[0].equalsIgnoreCase("rpc")) {
            //模拟通过消息方式异步转同步调用
            MqMsgBean mqMsgBean = new MqMsgBean();
            mqMsgBean.setTopic("MY_DEMO_REQUEST");
            mqMsgBean.setMsgBody(new HashMap<String, Object>() {{
                put("feeder", 10000L);
                put("stationId", 100);
            }});
            for (int i = 0; i < 10; i++) {
                new Thread(() -> {
                    EmsgRestClient emsgRestClient = new EmsgRestClient();
                    Object result = emsgRestClient.sendRequest(mqMsgBean);
                    System.out.println(emsgRestClient.getId()+" 调用结果：");
                    System.out.println(result);
                }).start();
            }
        }
        //emsg send,recv wait
        BaseRunner.waitUntilExit(100);
    }
}
